import re
from urllib import parse
from speech import speech
originFunc = None
def cancel():
    speech.cancelSpeech()
def speak(text, *args, **kwargs):
    if not originFunc:return
    def repl(item):
        if isinstance(item, str):
            msg = re.sub('(?:%[a-f0-9]{2})+', lambda x: parse.unquote(x.group()), item, flags = re.I)
            return re.sub(r'(?:\\u[a-f0-9]{4})+', lambda x: bytes(x.group(), 'raw_unicode_escape').decode('raw_unicode_escape'), msg, flags = re.I)
        return item
    msg = [repl(item) for item in text] if isinstance(text, list) else repl(text)
    originFunc(msg, *args, **kwargs)
def work():
    global originFunc
    if originFunc:return
    originFunc = speech.speak
    speech.speak = speak
def rest():
    global originFunc
    if not originFunc:return
    speech.speak = originFunc
    originFunc = None